package com.freaky.widget;

import com.freaky.paomian.R;

import android.R.integer;
import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.TimePicker;

public class AdvancedTimePicker extends TimePicker {

	private static final OnTimeChangedListener NO_OP_CHANGE_LISTENER = new OnTimeChangedListener() {

		@Override
		public void onTimeChanged(AdvancedTimePicker view, int minute,
				int second) {
			// TODO Auto-generated method stub

		}
	};

	// state
	private int mCurrentMinute = 0;// 0-99
	private int mCurrentSecond = 0;// 0-59

	// ui components
	private final NumberPicker mMinutePicker;
	private final NumberPicker mSecondPicker;

	// Callback
	private OnTimeChangedListener mOnTimeChangedListener;

	public interface OnTimeChangedListener {
		/**
		 * @param view
		 *            The view associated with this listener.
		 * 
		 * @param minute
		 *            The start minute
		 * 
		 * @param second
		 *            The start second
		 */
		void onTimeChanged(AdvancedTimePicker view, int minute, int second);
	}

	public AdvancedTimePicker(Context context) {
		this(context, null);
		// TODO Auto-generated constructor stub
	}

	public AdvancedTimePicker(Context context, AttributeSet attr) {
		this(context, attr, 0);
		// TODO Auto-generated constructor stub
	}

	public AdvancedTimePicker(Context context, AttributeSet attr, int defStyle) {
		// TODO Auto-generated constructor stub
		super(context, attr, defStyle);

		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.advanced_time_picker, this, true);

		mMinutePicker = (NumberPicker) findViewById(R.id.minute);
		mMinutePicker.setRange(0, 59);
		mMinutePicker.setSpeed(100);
		mMinutePicker.setFormatter(NumberPicker.TWO_DIGIT_FORMATTER);
		mMinutePicker.setOnChangeListener(new NumberPicker.OnChangedListener() {

			@Override
			public void onChanged(NumberPicker picker, int oldVal, int newVal) {
				// TODO Auto-generated method stub
				mCurrentMinute = newVal;

			}
		});

		mSecondPicker = (NumberPicker) findViewById(R.id.second);
		mSecondPicker.setRange(0, 59);
		mSecondPicker.setSpeed(100);
		mSecondPicker.setFormatter(NumberPicker.TWO_DIGIT_FORMATTER);
		mSecondPicker.setOnChangeListener(new NumberPicker.OnChangedListener() {

			@Override
			public void onChanged(NumberPicker picker, int oldVal, int newVal) {
				// TODO Auto-generated method stub
				mCurrentSecond = newVal;
			}
		});

		if (!isEnabled()) {
			setEnabled(false);
		}
	}

	@Override
	public void setEnabled(boolean enabled) {
		super.setEnabled(enabled);
		mMinutePicker.setEnabled(enabled);
		mSecondPicker.setEnabled(enabled);
	}

	/**
	 * Used to save / restore state of advanced time picker
	 */
	private static class SavedState extends BaseSavedState {

		private final int mMunite;
		private final int mSecond;

		/**
		 * Constructor called from {@link DatePicker#onSaveInstanceState()}
		 */
		private SavedState(Parcelable superState, int minute, int second) {
			super(superState);
			mMunite = minute;
			mSecond = second;
			// TODO Auto-generated constructor stub
		}

		/**
		 * Constructor called from {@link #CREATOR}
		 */
		private SavedState(Parcel in) {
			super(in);
			mMunite = in.readInt();
			mSecond = in.readInt();
		}

		public int getMinute() {
			return mMunite;
		}

		public int getSecond() {
			return mSecond;
		}

		@Override
		public void writeToParcel(Parcel dest, int flags) {
			super.writeToParcel(dest, flags);
			dest.writeInt(mMunite);
			dest.writeInt(mSecond);
		}

		public static final Parcelable.Creator<SavedState> CREATOR = new Creator<SavedState>() {
			public SavedState createFromParcel(Parcel in) {
				return new SavedState(in);
			}

			public SavedState[] newArray(int size) {
				return new SavedState[size];
			}
		};

	}

	@Override
	protected Parcelable onSaveInstanceState() {
		Parcelable superState = super.onSaveInstanceState();
		return new SavedState(superState, mCurrentMinute, mCurrentSecond);
	}

	@Override
	protected void onRestoreInstanceState(Parcelable state) {
		SavedState ss = (SavedState) state;
		super.onRestoreInstanceState(ss.getSuperState());

	}

	/**
	 * Set the callback that indicates the time has been adjusted by the user.
	 * 
	 * @param onTimeChangedListener
	 *            the callback, should not be null.
	 */
	public void setOnTimeChangedListener(
			OnTimeChangedListener onTimeChangedListener) {
		mOnTimeChangedListener = onTimeChangedListener;
	}

	public Integer getCurrentMinute() {
		return mCurrentMinute;
	}

	/**
	 * set current minute
	 */
	public void setCurrentMinute(Integer currentMinute) {
		this.mCurrentMinute = currentMinute;
		updateDisplayMinute();
	}

	/**
	 * Set the state of the spinners appropriate to the current minute.
	 */

	private void updateDisplayMinute() {
		mMinutePicker.setCurrent(mCurrentMinute);
		mOnTimeChangedListener.onTimeChanged(this, getCurrentMinute(),
				getCurrentSecond());
	}

	public Integer getCurrentSecond() {
		return mCurrentSecond;
	}

	/**
	 * set current second
	 */
	public void setCurrentSecond(Integer currentSecond) {
		this.mCurrentSecond = currentSecond;
		updateDisplaySecond();
	}

	/**
	 * Set the state of the spinners appropriate to the current second.
	 */
	private void updateDisplaySecond() {
		mSecondPicker.setCurrent(mCurrentSecond);
		mOnTimeChangedListener.onTimeChanged(this, getCurrentMinute(),
				getCurrentSecond());
	}
}
